<template>
	<view>
		<scan-layout ref="scanLayout"
		 :tabsList="tabsList"
		 sum-confirm-text="报备入库"
		  @add-click="addClick"
		   @confirm="confirmClick"
		   @product-change="productChange"
		   :current="currentTab"
		   @change="tabChange"
			>
	    </scan-layout>
		<u-back-top :scroll-top="scrollTop"></u-back-top>
	</view>
</template>

<script>
import ProductInfoListModal from './components/productInfoListModal/productInfoReport.vue';
import ScanLayout from './components/scanLayout/scanLayout.vue';
import { mapMutations, mapActions } from 'vuex';
export default {
	components: {
		ScanLayout,
		ProductInfoListModal
	},
	onLoad() {
		// obj
		// if (obj.single) {
		// 	//单锁收货
		// 	uni.setNavigationBarTitle({
		// 		title: '单锁收货'
		// 	});
		// }
	},
	data() {
		return {
			tabsList: [
				{
					name: '报备扫描'
				},
				{
					name: '数据汇总'
				},
			],
			no:'chenjianen',
			jsondata:[{ mcuid:'',xh:''}],
			form: {},
			scrollTop:0,
		};
	},
	beforeDestroy() {
		this.resetScanDeviceInfo();
		this.resetSumList();
	},
	computed: {
		canAdd() {
			return this.$store.state.storageOrder.canAdd;
		}
	},
	onPageScroll(e){
		this.scrollTop=e.scrollTop;
	},
	methods: {
		...mapActions({
			inStorageDeviceOther: 'storageOrder/inStorageDeviceOther', //创建
			getDeviceByCustomer: 'storageOrder/getDeviceByCustomer',//根据机身码和客户编号查询记录
			deviceInStorage: 'storageOrder/deviceInStorage' ,//更新入库状态
			// 判断是否在保
			getOrderReport: 'storageOrder/getOrderReport',
			// 报备
			getServiceReport: 'storageOrder/getServiceReport',
		}),
		...mapMutations({
			resetSumList: 'storageOrder/resetSumList',
			dropSumListItem: 'storageOrder/dropSumListItem',
			setSumList: 'storageOrder/setSumList',
			resetScanDeviceInfo: 'storageOrder/resetScanDeviceInfo',
			updateCanAdd: 'storageOrder/updateCanAdd',
			updateScanDeviceInfo: 'storageOrder/updateScanDeviceInfo',
			
		}),
		// tab栏
		tabChange(e) {
			console.log(e);
			this.currentTab = e;
		},
		// 提交添加按钮到列表
		addClick() {
			// if(this.form.mcuid==null||this.form.mcuid==""){
				// uni.showToast({
				// 		title: '添加不能为空',
				// 	});
			// } else{
				this.$refs.scanLayout.addToList();
			// }
		},
		// 查机身码是否存在在报备库中操作
		productChange(e) {
			let customerNo = this.$store.state.user.userInfo.teamNo;
			if (e.isValid) {
				// 根据机身码和客户编号查询记录
				// this.getDeviceByCustomer({
					// 判断是否报备
				this.getOrderReport({	
					 mcuid:this.$store.state.storageOrder.scanDeviceInfo.code,
				})
					.then(res => {
						console.log(res);
						// code == 0 mcuid == 0
						if (res && res.code == 0) {
							if (res.data === null) {
								//查不到，新建 0不在库中1在
								uni.showToast({
									title: '机身码未报备',
								});
								
							} 
							else if (!res.data.mcuid == null) {
								//已在库中
								// this.updateCanAdd(false);
								uni.showToast({
									title: '机身码已报备入库',
									icon: 'none'
								});
							} else {
								// this.updateCanAdd(true);
							}
						} else {
							uni.showToast({
								title: '查询机身码失败',
								icon: 'none'
							});
							this.updateCanAdd(false);
						}
					})
					.catch(err => {
						console.log(err);
						this.updateCanAdd(false);
						uni.showToast({
							title: '网络错误，查询机身码失败',
							icon: 'none'
						});
					});
			} else {
				
			}
		},
		
		// 确认报备入库操作
		confirmClick() {
			this.$confirm('提示', '确定报备入库？', this.serviceReport);
		},
		async serviceReport() {
			uni.showLoading({
				mask: true,
				title: '请求中'
			});
			let list = this.$store.state.storageOrder.sumList;
			console.log(list)
			let successCodeArr = [];
			let failCodeArr = [];
			
			//组装数据
			let jsondata = []
			list.map((item,index)=>{
				console.info(item)
				if(item.checked==true){
					jsondata.push({"mcuid":item.code,"xh":item.model,});
				}
			});
			console.info(jsondata)
			
			//申请报备
			let res = await this.getServiceReport({jsondata});
			
			uni.hideLoading();
			if (failCodeArr.length > 0) {
				let newList = [];
				list.forEach(e => {
					if (successCodeArr.indexOf(e.code) == -1) {
						newList.push(e);
					}
				});
				this.setSumList(newList);
				uni.showModal({
					title: `有${failCodeArr.length}条数据请求失败，请重新尝试`,
					showCancel: false
				});
			} else {
				this.dropSumListItem();
				uni.showToast({
					icon: 'success',
					title: '报备成功'
				});
			}
		}
	}
};
</script>

<style lang="less">
@import './styles/table.less';
</style>
